package cn.com.basic.framework.web.base.controller;

import cn.com.basic.framework.exception.enums.errors.SystemError;
import cn.com.basic.framework.web.base.entity.BaseEntity;
import cn.com.basic.framework.web.base.request.BaseQueryPage;
import cn.com.basic.framework.web.base.service.BaseCRUDService;
import cn.com.basic.framework.web.domain.RespResult;
import cn.com.basic.framework.web.error.BizException;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * BaseCRUDController
 * @Author Cuixk
 * 2020/1/9 10:39
 */
public class BaseCRUDController <M extends BaseCRUDService<T>, T extends BaseEntity> extends BaseController<T>  {


    @Autowired
    protected M baseService;

    /**
     *
     * @param entity
     * @return 错误消息  无错误消息返回null
     */
    protected SystemError checkForm(T entity, T oldEntity) {
        return null;
    }


    /**
     * 基础接口(参数和后台沟通)-根据id查询实体
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("find")
    @ApiOperation(value = "基础接口(参数和后台沟通)-根据id查询实体", httpMethod = "POST")
    public RespResult find(@RequestParam(value = "id") String id) {
        T entity = getBean(id);
        if (entity == null) {
            throw new BizException(SystemError.SYSTEM_TOLERANT_ERROR);
        }
        return RespResult.builder().data(entity).build();
    }

    /**
     * 基础接口(参数和后台沟通)-添加记录
     * @param entity
     * @return
     */
    @ResponseBody
    @PostMapping("add")
    @ApiOperation(value = "基础接口(参数和后台沟通)-添加接口", httpMethod = "POST")
    public RespResult add(@RequestBody @ApiParam T entity) {
        try {
            SystemError errorCodeEnum = checkForm(entity, null);
            if (errorCodeEnum != null) {
                throw new BizException(errorCodeEnum);
            }
            baseService.save(entity);
            return RespResult.builder().build();
        } catch (Exception e) {
            throw new BizException(SystemError.SYSTEM_EXECUTE_ERROR);
        }
    }

    /**
     * 基础接口(参数和后台沟通)-更新记录
     * @param entity
     * @return
     */
    @ResponseBody
    @PostMapping("update")
    @ApiOperation(value = "基础接口(参数和后台沟通)-更新接口", httpMethod = "POST")
    public RespResult update(@RequestBody @ApiParam T entity) {
        try {
            T oldEntity = getBean(entity.getId());
            if (oldEntity == null) {
                throw new BizException(SystemError.SYSTEM_EXECUTE_ERROR);
            }
            SystemError errorCodeEnum = checkForm(entity, oldEntity);
            if (errorCodeEnum != null) {
                throw new BizException(errorCodeEnum);
            }
            baseService.updateById(entity);
            return RespResult.builder().build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(SystemError.SYSTEM_EXECUTE_ERROR);
        }
    }


    /**
     * 基础接口(参数和后台沟通)-删除记录
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("delete")
    @ApiOperation(value = "基础接口(参数和后台沟通)-删除接口", httpMethod = "POST")
    public RespResult delete(@RequestParam(value = "id") String id) {
        try {
            T entity = getBean(id);
            if (entity == null) {
                throw new BizException(SystemError.SYSTEM_TOLERANT_ERROR);
            }
            baseService.removeById(entity);
            return RespResult.builder().build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(SystemError.SYSTEM_EXECUTE_ERROR);
        }
    }

    /**
     * 基础接口(参数和后台沟通)-分页接口
     * @return
     */
    @ResponseBody
    @PostMapping("list")
    @ApiOperation(value = "基础接口(参数和后台沟通)-分页接口", httpMethod = "POST")
    public RespResult list() {
        try {
            BaseQueryPage BaseQueryPage = getBaseQueryPage();
            IPage<T> queryResult = baseService.listByPage(BaseQueryPage);
            return RespResult.builder().data(queryResult).build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(SystemError.SYSTEM_EXECUTE_ERROR);
        }
    }

    /**
     * 基础接口(参数和后台沟通)-根据id批量删除接口
     * @param ids
     * @return
     */
    @ResponseBody
    @PostMapping("deleteBatchIds")
    @ApiOperation(value = "基础接口(参数和后台沟通)-根据id批量删除接口", httpMethod = "POST")
    public RespResult deleteBatchIds(@RequestBody @ApiParam List<String> ids) {
        try {
            if (ids == null || ids.size() == 0) {
                throw new BizException(SystemError.SYSTEM_RESOURCE_ERROR);
            }
            baseService.removeByIds(ids);
            return RespResult.builder().build();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(SystemError.SYSTEM_EXECUTE_ERROR);
        }
    }

    protected T getBean(String id) {
        try {
            if (StringUtils.isBlank(id)) {
                return null;
            }
            T model = baseService.getById(id);
            if (model == null) {
                return null;
            }
            return model;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
